let Level = require('Level');
let soundMgr = require('soundMgr');
let dragons = require('dragons');
let nodeMgr = require('nodeMgr');
let sounds = require('sounds');
let DragLayer = require('dragLayer');
let DragTips = require('DragTips');

let actions = require('actions');
cc.Class({
    extends: Level,

    properties: {
        tipsDelay:8,
        tipsNode:DragTips,
        nextLevelId:2,

        targetCount:9,

        sound1:'',
        sound2:'',
        sound3:'',
        sound4:'',
        dragLayer:DragLayer,
        effectNode:cc.Node,
    },
    // LIFE-CYCLE CALLBACKS:
    start () {
        nodeMgr.findNode('background/citybg').active = false;
        nodeMgr.findNode('background/shenlin').active = true;
    },
    /**
     * 开始关卡
     */
    startLevel(){

        let self = this;
        nodeMgr.disableTouch();
        soundMgr.playSoundEffectByName(self.sound1,function () {
            // soundMgr.playSoundEffectByName(self.sound2,function () {
            //
            // });
            nodeMgr.enableTouch();
            self._delayShowTips();
        });
        self.containers = [];
        self._nextQuestion();
    },
    _nextQuestion(){
        let self = this;
        // actions.moveTo(self.bus,3,30,self.bus.y,function () {
        //     dragons.playAnimation(self.bus,self.busIdleAnimation,0);
        // });
        self.curProgress = 0;
    },

    _delayShowTips(){
        let self = this;
        self.scheduleOnce(self._showTips,self.tipsDelay);
    },

    /**
     * 把提示显示在随机位置
     * @private
     */
    _showTips(){
        let self = this;
        self.tipsNode.node.active = true;
        soundMgr.playSoundEffectByName(sounds.PB_16);
    },
    _hideTips(){
        let self = this;
        self.tipsNode.node.active = false;
        self.unschedule(self._showTips);
    },

    /**
     * 拖拽开始，一般在此步骤取消引导
     * @param data
     */
    onDragStart(data){
        let self = this;
        self._hideTips();
    },
    /**
     * 拖拽成功
     * @param data
     * {
     *     container:目标容器
     *     drag:被拖拽节点
     * }
     */
    onDragSuccess(data){
        let self = this;
        soundMgr.playSoundEffectByName('tongyong_right');
        let target = data.target;
        let fadeOut = cc.fadeOut(0.1);
        target.node.runAction(fadeOut);

        let container = data.container;
        let index = self.dragLayer.containers.indexOf(container);
        if (index >= 0){
            self.containers.push(container);
            self.dragLayer.containers.splice(index,1);
        }

        self._updateMapzi(data);
        self.curProgress++;
        if (self.curProgress >= self.targetCount){//达成目标
            // dragons.playAnimation(self.bus,self.busRunAnimation,0);
            actions.delayTime(self.node,0.5,function () {
                self._allHappy();
                nodeMgr.disableTouch();
                soundMgr.playSoundEffectByName(self.sound4,function () {
                    nodeMgr.enableTouch();
                    if (self.nextLevelId>0){
                        self.loadLevel(self.nextLevelId);
                    }else{
                        self._gameOver();
                    }
                });
            });
        }
    },

    _updateMapzi(data){
        let self = this;
        let container = data.container;
        let node = container.node;

        let target = data.target;
        let groupId = target.groupId;
        let idx = groupId.charAt(1);
        let type = groupId.charAt(0);
        let pre = 'sodier_';
        if (type === 'k'){
            pre = 'king_';
        }
        let animationName = pre+'gaoxing_'+idx;
        if (idx == 2){
            animationName = pre+'gaoxinge_'+idx;
        }
        let idleName = pre+'idle_'+idx;
        cc.log('animationName:',animationName);
        dragons.playAnimation(node.getChildByName('animation'),animationName,3,function () {
            dragons.playAnimation(node.getChildByName('animation'),idleName,0);
        });

        let cellId = target.cellId;
        self._setMaozi(node,cellId);

        self.effectNode.x = node.x-35;
        self.effectNode.y = node.y-80;
        self.effectNode.active = true;
        dragons.playAnimation(self.effectNode,'newAnimation',1,function () {
            self.effectNode.active = false;
        });
    },
    _setMaozi(node,name){
        let ad = node.getChildByName('animation').getComponent(dragonBones.ArmatureDisplay);
        let armature = ad.armature();
        let names = [
            'caomao_hong',
            'caomao_huang',
            'caomao_lan',
            'mianmao_lan',
            'mianmao_hong',
            'mianmao_huang',
            'leifeng_huang',
            'leifeng_hong',
            'leifeng_lan',
        ]
        let index = names.indexOf(name);
        let slotMapzi = armature.getSlot('caomao_hong');
        slotMapzi.displayIndex = index;
        let xu = armature.getSlot('m11_3');
        xu.visible = false;
    },
    /**
     * 拖拽错误
     * @param data
     * {
     *     container:目标容器
     *     drag:被拖拽节点
     * }
     */
    onDragWrong(data) {
        let self = this;
        soundMgr.playSoundEffectByName('tongyong_error');
        nodeMgr.disableTouch();
        soundMgr.playSoundEffectByName(self.sound3,function () {
            nodeMgr.enableTouch();
        });
        let container = data.container;
        let node = container.node;
        let groupId = container.groups[0];

        // let target = data.target;
        // let groupId = target.groupId;
        let idx = groupId.charAt(1);
        let type = groupId.charAt(0);
        let pre = 'sodier_';
        if (type === 'k'){
            pre = 'king_';
        }
        let animationName = pre+'nonono_'+idx;
        let idleName = pre+'idle_'+idx;
        dragons.playAnimation(node.getChildByName('animation'),animationName,1,function () {
            dragons.playAnimation(node.getChildByName('animation'),idleName,0);
        });
    },
    _allHappy(){
        let self = this;
        let containers = self.containers;
        for (let i=0;i<containers.length;i++){
            let container = containers[i];
            let node = container.node;
            let groupId = container.groups[0];
            // let target = data.target;
            // let groupId = target.groupId;
            let idx = groupId.charAt(1);
            let pre = 'sodier_';
            let animationName = pre+'happy_'+idx;
            let idleName = pre+'idle_'+idx;
            dragons.playAnimation(node.getChildByName('animation'),animationName,0);
        }

    }
    // update (dt) {},
});
